package com.yq.ylmall.search.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.yq.ylmall.dto.search.QueryPageDto;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.search.document.ProductDocument;
import com.yq.ylmall.search.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "商品检索")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/search")
public class SearchController {

    private final IProductService productService;

    @GetMapping("/index")
    @ApiOperation(value = "首页数据，根据权重拿20条")
    @SentinelResource(value = "searchIndex")
    public Result<List<SearchHit<ProductDocument>>> getProduct() {
        return Result.ok(productService.getIndexBySort());
    }

    @PostMapping("/product")
    @ApiOperation(value = "商品检索")
    @SentinelResource(value = "searchProduct")
    public Result<List<SearchHit<ProductDocument>>> searchProduct(@RequestBody QueryPageDto dto) {
        return Result.ok(productService.query(dto));
    }

    @GetMapping("/{q}")
    @ApiOperation(value = "关键字检索")
    @SentinelResource(value = "searchKeyword")
    public Result<List<String>> searchKeyword(@PathVariable String q) {
        return Result.ok(productService.queryKeyword(q));
    }

}

